<?php

namespace Admin\Controller;

class WithdrawController extends AdminController {

    public function index() {
        $user = I('user', '');
        $account = I('account');

        $where = '1';
        if ($user) {
            if (isMobile($user)) {
                $where .= " AND user.mobile = '{$user}'";
            } else if (is_numeric($user)) {
                $where .= " AND user.id = '{$user}'";
            }
        }
        if ($account) {
            $where .= " AND ( withdraw.account = '{$account}' OR withdraw.account_name = '{$account}')";
        }

        $total = M('Withdraw')->query(" SELECT COUNT(*) AS total FROM __WITHDRAW__ `withdraw` LEFT JOIN __USER__ `user` ON `withdraw`.user_id = `user`.id WHERE {$where}");
        $total = $total[0]['total'];
        $Page = new \Think\Page($total, 20);
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $result = M('Withdraw')->query("SELECT `user`.nickname,`user`.mobile,`withdraw`.*  FROM __WITHDRAW__ `withdraw` LEFT JOIN __USER__ `user` ON `withdraw`.user_id = `user`.id WHERE {$where} ORDER BY `withdraw`.id DESC LIMIT {$limit}");
        $this->assign('result', $result);
		$this->assign('page', $Page->show());
        $this->display();
    }

    public function waiting() {
        $user = I('user', '');
        $account = I('account');

        $where = 'A.status = 0';
        
        $total = M('Withdraw')->alias('A')->where($where)->count();
        $Page = new \Think\Page($total, 20);
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $result = M('Withdraw')->alias('A')->field('A.*,B.nickname,B.mobile')
                ->join(C('DB_PREFIX').'user B ON A.user_id = B.id','left')
                ->where($where)->order('id desc')->limit($limit)->select();
        $this->assign('result', $result);
		$this->assign('page', $Page->show());
        $this->display();
    }

    public function change_status() {
        $ids = I('id', 0, 'intval');
        $status = I('status', '');
        $remark = I('remark');
        if (!$ids) {
            $this->error('参数错误');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }

        if (!is_numeric($status) || !in_array($status, [1, -1])) {
            $this->error('参数错误');
        }


        $result = M('Withdraw')->where(['id' => ['in', $ids]])->select();
        $count = 0;
        foreach ($result as $key => $val) {
            if ($status == 1) { //修改為已支付 
                if ($val['status'] == 0) { //只能更改状态为等待处理的的                    
                    M('Withdraw')->where(['id' => $val['id']])->data(['status' => $status, 'update_time' => time(), 'remark' => $remark])->save();
//                    $yesMonth = mktime(0, 0, 0, date('m', $val['add_time']), 1) - 1;  //上个月和之前  
//                    $where = [
//                        'user_id' => $val['user_id'],
//                        'lock' => 1,
//                        'withdraw' => 1,
//                        'add_time' => ['elt', $yesMonth]
//                    ];
//                    M('Account')->where($where)->setField('lock', 0);
                    $content = '您申请的提现￥' . $val['amount'] . '已完成，请注意查收';
                    $messageData = [
                        'user_id' => $val['user_id'],
                        'title' => $content,
                        'content' => $content,
                        'f_user_id' => 0,
                        'add_time' => time(),
                        'read' => 0,
                    ];
                    M('Message')->data($messageData)->add();
                    $count++;
                } else {
                    continue;
                }
            } else if ($status == -1) {
                
                if ($val['status'] == 0) {  //只能更改状态为等待处理
                    M('Withdraw')->where(['id' => $val['id']])->data(['status' => $status, 'update_time' => time(), 'remark' => $remark])->save();
                    $content = '您申请的提现￥' . $val['amount'] . '被退回';
                    if ($remark) {
                        $content .= "【" . $remark . "】";
                    }
                    $messageData = [
                        'user_id' => $val['user_id'],
                        'title' => $content,
                        'content' => $content,
                        'f_user_id' => 0,
                        'add_time' => time(),
                        'read' => 0,
                    ];
                    M('Message')->data($messageData)->add();
//                    $yesMonth = mktime(0, 0, 0, date('m', $val['add_time']), 1) - 1;  //上个月和之前  
//                    $where = [
//                        'user_id' => $val['user_id'],
//                        'lock' => 1,
//                        'withdraw' => 1,
//                        'add_time' => ['elt', $yesMonth]
//                    ];
//                    M('Account')->where($where)->data(['withdraw' => 0, 'lock' => 0])->save();
                    M('User')->where(array('id'=>$val['user_id']))->setInc('cash',$val['amount']);
                    $count++;
                } else {
                    continue;
                }
            }
        }
        if ($count == 0) {
            $this->error('没有处理任何数据');
        }
        $this->success("共{$count}条数据处理成功！");
    }

    
      /**
     * 提现排行榜
     */
    public function rank(){ 
        $result = M('Withdraw')->field('sum(amount) as amount,count(*) as total,user_id,max(add_time) as add_time')->group("user_id")->where('status >= 0')->limit(20)->order('amount DESC')->select();
        foreach($result as $key=>$val){ 
            $user_info = M('User')->field('nickname,avatar,mobile')->where(['id'=>$val['user_id']])->find();
            $result[$key]['nickname'] = $user_info['nickname'];
            $result[$key]['avatar'] = $user_info['avatar'];
            $result[$key]['mobile'] = $user_info['mobile'];
            $result[$key]['invite_total'] = M('User')->where(['invite_first'=>$val['user_id']])->count();
        }
        $this->assign('result',$result);
        $this->display();
    }
}
